package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.GoodsCategoryService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.GoodsCategory;
import com.qdlc.p2p.dal.model.GoodsCategoryModel;

/**
 * 商品类别管理
 * @author xxx
 * @version 2.0
 * @date 2015年6月17日 上午10:11:41
 */
public class ManageGoodsCategoryAction extends BaseAction<GoodsCategoryModel> implements ModelDriven<GoodsCategoryModel> {
	
	@Resource
	private GoodsCategoryService goodsCategoryService;
	
	private Map<String, Object> data;
	
	@Action("/modules/user/score/goodsCategory/manage")
	public String manage(){
		return "manage";
	}
	
	@Action("/modules/user/score/goodsCategory/list")
	public void list() throws Exception {
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(99);
		}
		List<GoodsCategory> list = goodsCategoryService.getCategoryList(model);
		data = new HashMap<String, Object>();
		data.put("total", 0);
		data.put("rows", list);
		printJson(getStringOfJpaObj(data));
	}
	
	@Action("/modules/user/score/goodsCategory/checkCategoryName")
	public void checkCategoryName(){
		goodsCategoryService.checkCategoryName(model.getName());
	}
	
	@Action("/modules/user/score/goodsCategory/append")
	public String append(){
		return "append";
	}
	
	@Action("/modules/user/score/goodsCategory/doAppend")
	public void doAppend() throws Exception{
		goodsCategoryService.save(model.prototype());
		printSuccess();
	}
	
	@Action("/modules/user/score/goodsCategory/modify")
	public String modify() {
		GoodsCategory category = goodsCategoryService.find(model.getId());
		request.setAttribute("category", category);
		return "modify";
	}
	
	@Action("/modules/user/score/goodsCategory/doModify")
	public void doModify() throws Exception {
		goodsCategoryService.update(model.prototype());
		printSuccess();
	}
	
	@Action("/modules/user/score/goodsCategory/disable")
	public void disable() throws Exception{
		GoodsCategory goodsCategory = goodsCategoryService.find(model.getId());
		goodsCategory.setStatus(GoodsCategoryModel.STATUS_DISABLE);
		goodsCategoryService.update(goodsCategory);
		printSuccess();
	}
	
	@Action("/modules/user/score/goodsCategory/enable")
	public void enable() throws Exception{
		GoodsCategory goodsCategory = goodsCategoryService.find(model.getId());
		goodsCategory.setStatus(GoodsCategoryModel.STATUS_ENABLE);
		goodsCategoryService.update(goodsCategory);
		printSuccess();
	}
}
